package com.tianxin.workbao.workBao.adapter;

import android.content.Context;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.tianxin.workbao.workBao.R;
import com.tianxin.workbao.workBao.bean.TransPrice;
import com.tianxin.workbao.workBao.holder.EmptyHolder;
import com.tianxin.workbao.workBao.utils.common.TimeUtils;

import java.util.List;

/**
 * Created by hacker on 2017/7/26.
 */

public class TransPriceAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private static final int TYPE_EMPTY = 0;
    private static final int TYPE_DEFAULT = 1;
    private List<TransPrice> mTransPrice;
    private Context mContext;

    public TransPriceAdapter(List<TransPrice> list, Context context) {
        this.mTransPrice = list;
        this.mContext = context;
    }

    @Override
    public int getItemViewType(int position) {
        if (mTransPrice.size() > 0) {
            return TYPE_DEFAULT;
        } else {
            return TYPE_EMPTY;
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = null;
        if (viewType == TYPE_DEFAULT) {
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.trans_price_item, parent, false);
            return new ViewHolder(view);
        } else {
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.empty, parent, false);
            return new EmptyHolder(view);
        }

    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof ViewHolder) {
            ViewHolder viewHolder = (ViewHolder) holder;
            viewHolder.BindViewData(mTransPrice.get(position));
            viewHolder.setViewListener(position);
        }
    }

    @Override
    public int getItemCount() {
        if (mTransPrice.size() > 0) {
            return mTransPrice.size();
        } else {
            return 1;
        }
    }

    public int getItemColumnSpan(int position) {
        switch (getItemViewType(position)) {
            case TYPE_DEFAULT:
                return 1;
            default:
                return 0;
        }
    }

    class ViewHolder extends RecyclerView.ViewHolder {

        TextView startTimeTV;
        TextView inPriceTV;
        TextView outPriceTV;
        TextView lossValveTV;
        TextView lossPriceTV;
        TextView user;

        public ViewHolder(View itemView) {
            super(itemView);
            startTimeTV = (TextView) itemView.findViewById(R.id.start_time);
            inPriceTV = (TextView) itemView.findViewById(R.id.in_price);
            outPriceTV = (TextView) itemView.findViewById(R.id.out_price);
            lossValveTV = (TextView) itemView.findViewById(R.id.loss_value);
            lossPriceTV = (TextView) itemView.findViewById(R.id.out_loss_price);
            user = (TextView) itemView.findViewById(R.id.user);
        }

        public void BindViewData(final TransPrice transPrice) {
            if (transPrice.getEndtime() != null) {
                startTimeTV.setText(TimeUtils.parseDateTime(transPrice.getStarttime())
                        + " 到 " +
                        TimeUtils.parseDateTime(transPrice.getEndtime()));
            } else {
                startTimeTV.setText(TimeUtils.parseDateTime(transPrice.getStarttime()));
            }

            inPriceTV.setText(transPrice.getIntransunitprice() + "");
            outPriceTV.setText(transPrice.getUnitprice() + "");
            lossValveTV.setText(transPrice.getLossvalue() + "");
            lossPriceTV.setText(transPrice.getOutlossunitprice() + "");
        }

        public void setViewListener(final int position) {

        }
    }
}
